package tankgame01;

import java.io.*;
import java.util.Vector;

public class Recorder {
    private static int enemyTankNum = 0;
    private static BufferedReader br = null;
    private static BufferedWriter bw = null;
    private static String recordPath = "src\\record.txt";
    private static Vector<EnemyTank> enemyTanks = null;
    private static Vector<lastInfo> storeEnemytanks = new Vector<>();

    public static void setEnemyTanks(Vector<EnemyTank> enemyTanks) {
        Recorder.enemyTanks = enemyTanks;
    }

    public static String getRecordPath() {
        return recordPath;
    }

    public static int getEnemyTankNum() {
        return enemyTankNum;
    }
    public static Vector<lastInfo> getStoreEnemyTanksAndNum(){
        try {
            br = new BufferedReader(new FileReader(recordPath));
            enemyTankNum = Integer.parseInt(br.readLine());
            String line = "";
            while((line = br.readLine()) != null){
                String[] xyd = line.split(" ");
                lastInfo info = new lastInfo(Integer.parseInt(xyd[0]),
                        Integer.parseInt(xyd[1]),Integer.parseInt(xyd[2]));
                storeEnemytanks.add(info);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return storeEnemytanks;
    }

    public static void setEnemyTankNum(int enemyTankNum) {
        Recorder.enemyTankNum = enemyTankNum;
    }
    public static void add(){
        enemyTankNum++;
    }
    public static void removeRecord(){
        new File(recordPath).delete();
    }
    public static void exitRecord()  {
        try {
            bw = new BufferedWriter(new FileWriter(recordPath));
            bw.write(enemyTankNum + "\r\n");
            for (int i = 0; i < enemyTanks.size();i++){
                EnemyTank enemyTank = enemyTanks.get(i);
                String record = enemyTank.getX() + " " + enemyTank.getY() + " " + enemyTank.getDirection() + "\r\n";
                bw.write(record);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (bw != null) {
                    bw.close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
